﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using RMS.Models;
using System.Web.Security;

namespace RMS.Controllers
{
    public class HomeController : Controller
    {
        RMSDataClassesDataContext db = new RMSDataClassesDataContext();
        public ActionResult ResetDatabase()
        {
            foreach (var item in Membership.GetAllUsers())
            {
                Membership.DeleteUser(item.ToString());
            }
            Roles.DeleteRole("Administrator");
            Roles.DeleteRole("Customer");
            Roles.DeleteRole("Company");
            Roles.DeleteRole("Moderator");
            Roles.CreateRole("Administrator");
            Roles.CreateRole("Customer");
            Roles.CreateRole("Company");
            Roles.CreateRole("Moderator");
            Membership.CreateUser("admin", "admin", "admin@cmuras.com");

            User adminUser = new Models.User
            {
                IsAdmin = true,
                UserName = "admin",
                Name = "System Administrator"

            };
            db.CreateDatabase();
            db.Users.InsertOnSubmit(adminUser);
            db.SubmitChanges();
            
            Roles.AddUserToRole("admin", "Administrator");
            db.ProblemGroups.InsertOnSubmit(new ProblemGroup
            {
                Name = "สินค้าเสียหาย"
            });
            db.ProblemGroups.InsertOnSubmit(new ProblemGroup
            {
                Name = "สินค้าล่าช้า"
            });
            db.ProblemGroups.InsertOnSubmit(new ProblemGroup
            {
                Name = "เกิดอุบัติเหตุ"
            });
            db.ProblemGroups.InsertOnSubmit(new ProblemGroup
            {
                Name = "ต้นทุนสูงผิดปกติ"
            });

            db.BestPracticeGroups.InsertOnSubmit(new BestPracticeGroup
            {
                Name = "หลีกเลี่ยง"
            });
            db.BestPracticeGroups.InsertOnSubmit(new BestPracticeGroup
            {
                Name = "ควบคุม"
            });
            db.BestPracticeGroups.InsertOnSubmit(new BestPracticeGroup
            {
                Name = "คงไว้"
            });
            db.BestPracticeGroups.InsertOnSubmit(new BestPracticeGroup
            {
                Name = "ถ่ายโอน"
            });

            db.RiskTypes.InsertOnSubmit(new RiskType
            {
                Name = "Technical Risk"
            });
            db.RiskTypes.InsertOnSubmit(new RiskType
            {
                Name = "Market Risk"
            });
            db.RiskTypes.InsertOnSubmit(new RiskType
            {
                Name = "Financial Risk"
            });
            db.RiskTypes.InsertOnSubmit(new RiskType
            {
                Name = "Human Risk"
            });
            db.SubmitChanges();
            
            
            return RedirectToAction("Index");
        }
        public ActionResult Index()
        {
            ViewBag.Message = "Welcome to CMU Risk Management System";

            return View();
        }
        public ActionResult About()
        {
            return View();
        }
    }
}
